<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use tp_tools\Curl;

class Upload extends Model
{

    public function publishFile($path_row, $savename, $user_id, $service_id, $mime_type, $upload_size = 0)
    {
        $curl = new Curl;
        $upfile = ['upfile' => new \CURLFile($path_row, $mime_type)];
        $remote_url = config('remote_url');
        $remote = $curl->post($remote_url, $upfile, true);
        $remote = json_decode($remote, true);
        if ($remote['data']['status'] == 200) {
            $row = [
                'album_id' => 0,
                'user_id' => $user_id,
                'service_id' => $service_id,
                'upload_path' => $remote['data']['url'],
                'upload_url' => $remote['data']['url'],
                'upload_thumbs' => '',
                'upload_size' => $upload_size,
                'upload_mime_type' => $mime_type,
                'upload_metadata' => '',
                'upload_name' => $savename,
                'upload_time' => time()
            ];
            Db::table('cn_upload_service_base')->insert($row);
            return $remote['data']['url'];
        } else {
            return null;
        }
    }
}

?>